package com.macro.mall.bo;

import com.macro.model.UmsAdmin;
import com.macro.model.UmsResource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  spring-security 需要的用户实体类
 */

public class AdminDetails implements UserDetails {
    private UmsAdmin umsAdmin;
    private List<UmsResource> resources;

    public AdminDetails(UmsAdmin admin,List<UmsResource> resources){
        this.umsAdmin = admin;
        this.resources = resources;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 返回当前用户可以访问的资源
        return resources.stream()
                .map(role -> new SimpleGrantedAuthority(role.getId() + ":" + role.getName()))
                .collect(Collectors.toList());
//        System.out.println(resources);
//        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//        for(UmsResource resource : resources){
//            if (resource == null)  continue;
//            Long id = resource.getId();
//            String name = resource.getName();
//            authorities.add(new SimpleGrantedAuthority(id + ":" + name));
//        }
//        return authorities;
    }

    @Override
    public String getPassword() {
        return umsAdmin.getPassword();
    }

    @Override
    public String getUsername() {
        return umsAdmin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
       return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
